package controllers

import (
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/start/inits"

	"github.com/gin-gonic/gin"
)

// 初始化
func NewCommon(context *gin.Context, o string) *structs.Common {
	// 登录信息
	loginUser := GetLoginUserFromHead(context)

	// IP
	ip := utils.ClientIp(context.Request)

	// 返回
	return &structs.Common{
		Context:   context,
		Claim:     loginUser,
		Operation: o,
		Ip:        ip,
	}
}

// GetLoginUserFromHead 从COOKIE中获取用户信息
func GetLoginUserFromHead(context *gin.Context) *structs.AuthClaim {
	// rsp
	loginUser := &structs.AuthClaim{}

	//返回相应的登录信息
	loginUserJson := context.Request.Header.Get(utils.GetConfigValue(inits.LOGIN_USER_KEY))

	if len(loginUserJson) < 0 {
		return loginUser
	}
	err := utils.FromJson(loginUserJson, loginUser)
	if err != nil {
		return loginUser
	}
	return loginUser
}
